<template>
  <div class="permission-container">
    <div class="app-container">
      <!-- 表格 -->
      <el-card>
        <div style="text-align: right; margin-bottom: 20px">
          <el-button
            type="primary"
            size="small"
            @click="handleOpenAddDialog(1, '0')"
            >添加权限</el-button
          >
        </div>
        <el-table
          border
          stripe
          style="margin-top: 20px"
          :data="permtreeData"
          row-key="id"
          default-expand-all
        >
          <el-table-column label="名称" prop="name" />
          <el-table-column label="标识" prop="code" />
          <el-table-column label="描述" prop="description" />
          <el-table-column label="操作">
            <template #default="{ row }">
              <el-button
                v-if="row.type === 1"
                size="small"
                type="text"
                @click="handleOpenAddDialog(2, row.id)"
                >添加权限点</el-button
              >
              <el-button
                size="small"
                type="text"
                @click="handleOpenEditDailog(row.id)"
                >编辑</el-button
              >
              <el-button
                size="small"
                type="text"
                @click="handleDelDialog(row.id)"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>
      </el-card>
      <!-- 新增弹框 -->
      <el-dialog
        title="新增权限(点)"
        :visible="showAddDialog"
        @close="handleAddClose"
      >
        <el-form label-width="80px">
          <el-form-item label="权限名称">
            <el-input style="width: 95%" v-model="permForm.name" />
          </el-form-item>
          <el-form-item label="权限标识">
            <el-input style="width: 95%" v-model="permForm.code" />
          </el-form-item>
          <el-form-item label="权限描述">
            <el-input style="width: 95%" v-model="permForm.description" />
          </el-form-item>
          <el-form-item label="权限启用">
            <!-- 开关组件 -->
            <el-switch
              v-model="permForm.enVisible"
              active-value="1"
              active-color="#13ce66"
              inactive-color="#ff4949"
              inactive-value="0"
            />
          </el-form-item>
        </el-form>
        <template #footer>
          <el-button @click="handleAddClose">取消</el-button>
          <el-button type="primary" @click="handleAdd">确定</el-button>
        </template>
      </el-dialog>
      <!-- 编辑弹框 -->
      <el-dialog
        title="编辑权限(点)"
        :visible="showEditDialog"
        @close="handleEditClose"
      >
        <el-form label-width="80px">
          <el-form-item label="权限名称">
            <el-input style="width: 80%" v-model="permForm.name" />
          </el-form-item>
          <el-form-item label="权限标识">
            <el-input style="width: 80%" v-model="permForm.code" />
          </el-form-item>
          <el-form-item label="权限描述">
            <el-input style="width: 80%" v-model="permForm.description" />
          </el-form-item>
          <el-form-item label="权限启用">
            <!-- 开关组件 -->
            <el-switch
              v-model="permForm.enVisible"
              active-value="1"
              active-color="#13ce66"
              inactive-color="#ff4949"
              inactive-value="0"
            />
          </el-form-item>
        </el-form>
        <template #footer>
          <el-button @click="handleEditClose">取消</el-button>
          <el-button type="primary" @click="handleEdit">确定</el-button>
        </template>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import {
  getPermListAPI,
  addPermAPI,
  getPermDetailAPI,
  editPermAPI,
  delPermAPI,
} from "@/api/permissions";
import { list2Tree } from "@/utils/shift"; //导入树形组件
export default {
  name: "PermissionView",
  data() {
    return {
      //权限树形数组
      permtreeData: [],
      //控制新增权限弹框的显示
      showAddDialog: false,
      // 控制编辑权限弹框的显示
      showEditDialog: false,
      //权限表单
      permForm: {
        name: "",
        code: "",
        description: "",
        enVisible: "1",
      },
    };
  },
  created() {
    //创建钩子函数
    this.getPermList();
  },
  methods: {
    //获得权限列表
    async getPermList() {
      //发起获取列表请求
      const resp = await getPermListAPI();
      // console.log(resp);
      //把列表转化为树形
      this.permtreeData = list2Tree(resp.data, "0");
    },
    //新增权限弹框关闭
    handleAddClose() {
      this.showAddDialog = false;
    },
    //新增权限
    async handleAdd() {
      //校验
      //发起新增请求
      await addPermAPI({
        ...this.permForm, //展开对象
        type: this.type,
        pid: this.pid,
      });
      //关闭弹框
      this.handleAddClose();
      //获取权限列表
      this.getPermList();
      //给个提示
      this.$message.success("新增权限成功");
    },
    //新增权限弹框打开时
    handleOpenAddDialog(type, pid) {
      // type: 1-> 页面访问权，2 -> 按钮操作权
      // pid: 父级id，0 表示页面访问权，否则就是按钮
      //保存type和pid，给handleAdd使用
      this.type = type;
      this.pid = pid;
      //显示弹框
      this.showAddDialog = true;
      // this.getPermList = "";
    },
    //编辑权限弹框关闭
    handleEditClose() {
      this.showEditDialog = false;
    },
    // 编辑权限弹框打开
    async handleOpenEditDailog(id) {
      // 显示弹框
      this.showEditDialog = true;
      // 发起获取详情的请求
      const resp = await getPermDetailAPI(id);
      // 赋值(做回显)
      this.permForm = resp.data;
    },
    // 编辑权限
    async handleEdit() {
      // 注意：建议在调用所有api接口函数的时候，都主动捕捉错误
      // 用try-catch语句块捕获，这样可以保证代码的健壮性
      try {
        // 校验
        // 发起编辑请求
        await editPermAPI(this.permForm);
        // 关闭弹框
        this.handleEditClose();
        // 获取权限列表
        this.getPermList();
      } catch (err) {
        //给个系统数据编辑不了的提示
        this.$message.error(err.message || "操作失败");
      }
    },
    //删除权限
    async handleDelDialog(id) {
      try {
        // console.log(id);
        //询问this.$confirm('自定义文字内容')
        await this.$confirm("确认要删除该名称吗?", "温馨提示", {
          type: "error", //提示
        });
        //发送请求调接口
        await delPermAPI(id);
        // 重新获取角色列表
        this.getPermList();
        // 给个成功的提示
        this.$message.success("删除成功");
      } catch (err) {
        //给个删除不了的数据个提示
        this.$message.error(err.message || "操作失败");
      }
    },
  },
};
</script>

<style></style>
